#include "GaussianBlurEffect.h"

#include "Poco/Exception.h"
#include <stdlib.h>
#include <iostream>

using Poco::Exception;
using cv::GaussianBlur;
using cv::BORDER_CONSTANT;
using cv::BORDER_DEFAULT;
using cv::BORDER_ISOLATED;
using cv::BORDER_REFLECT;
using cv::BORDER_REFLECT_101;
using cv::BORDER_REPLICATE;
using cv::BORDER_TRANSPARENT;
using cv::BORDER_WRAP;

using namespace std;

GaussianBlurEffect::GaussianBlurEffect(string kSize, string sigmaX, string sigmaY, string borderType)
{
    // Parse kSize
    if(kSize=="3" || kSize=="5" || kSize=="7" || kSize=="9" || kSize=="11")
    {
        int n = atoi(kSize.c_str());
        this->kSize = Size(n,n);
    }
    else
        throw Exception(string("GaussianBlurEffect unknown kSize value: ") + kSize);

    // Parse sigmaX/Y
    this->sigmaX = atof(sigmaX.c_str());
    this->sigmaY = atof(sigmaY.c_str());
        
    // Parse borderType
    if(borderType=="BORDER_CONSTANT")
        this->borderType = BORDER_CONSTANT;
    else if(borderType=="BORDER_DEFAULT")
        this->borderType = BORDER_DEFAULT;
    else if(borderType=="BORDER_ISOLATED")
        this->borderType = BORDER_ISOLATED;
    else if(borderType=="BORDER_REFLECT")
        this->borderType = BORDER_REFLECT;
    else if(borderType=="BORDER_REFLECT_101")
        this->borderType = BORDER_REFLECT_101;
    else if(borderType=="BORDER_REPLICATE")
        this->borderType = BORDER_REPLICATE;
    else if(borderType=="BORDER_TRANSPARENT")
        this->borderType = BORDER_TRANSPARENT;        
    else if(borderType=="BORDER_WRAP")
        this->borderType = BORDER_WRAP;                
    else
        throw Exception(string("GaussianBlurEffect unknown borderType value: ") + borderType);
}

Mat GaussianBlurEffect::run(const Mat& src)
{
    cout << "Running GaussianBlurEffect" << endl;
    Mat dst;
    GaussianBlur(src, dst, this->kSize, this->sigmaX, this->sigmaY, this->borderType);
    return dst;
}